<?php

namespace plugin\admin\app\controller;

use plugin\admin\app\constant\v1\CommonConstant;
use plugin\admin\app\service\v1\NodeService;
use plugin\admin\app\service\v1\ServerService;
use support\Request;
use support\Response;
use plugin\admin\app\model\V1Server;
use plugin\admin\app\controller\Crud;
use support\exception\BusinessException;

/**
 * 服务器管理
 */
class V1ServerController extends Crud
{
    /**
     * 无需登录及鉴权的方法
     * @var array
     */
    protected $noNeedLogin = ['select2'];

    /**
     * 需要登录无需权的方法
     * @var array
     */
    protected $noNeedAuth = [];

    /**
     * @var V1Server
     */
    protected $model = null;

    /**
     * 构造函数
     * @return void
     */
    public function __construct()
    {
        $this->model = new V1Server;
    }

    /**
     * 浏览
     * @return Response
     */
    public function index(): Response
    {
        return view('v1-server/index');
    }

    /**
     * 查询
     * @param Request $request
     * @return Response
     * @throws BusinessException
     */
    public function select(Request $request): Response
    {
//        [$where, $format, $limit, $field, $order] = $this->selectInput($request);
//        $where['deleted'] = CommonConstant::DELETE_0;
//        $query = $this->doSelect($where, $field, $order);
//        $paginator = $query->paginate($limit);
//        $total = $paginator->total();
//        $items = $paginator->items();
//        foreach ($items as &$item) {
//            $used_number = NodeService::getServerNodeUsedNumber($item['id']);
//            $item['used_number'] = $used_number;
//        }
//        return json(['code' => 0, 'msg' => 'ok', 'count' => $total, 'data' => $items]);

        [$where, $format, $limit, $field, $order] = $this->selectInput($request);
        $where['deleted'] = CommonConstant::DELETE_0;
        $query = $this->doSelect($where, $field, $order);
        $query->withCount([
            'orderNode as number' => function ($query) {
                $query->where('deleted', CommonConstant::DELETE_0)
                    ->whereIn('status', [CommonConstant::DEFAULT_0, CommonConstant::DEFAULT_1])
                    ->whereIn('refund_status', [CommonConstant::DEFAULT_0, CommonConstant::DEFAULT_1,CommonConstant::DEFAULT_3])
                    ->select($query->raw("count(id)"));
            },
        ]);
        $paginator = $query->paginate($limit);
        $total = $paginator->total();
        $items = $paginator->items();
        return json(['code' => 0, 'msg' => 'ok', 'count' => $total, 'data' => $items]);
//        return $this->doFormat($query, $format, $limit);
    }

    /**
     * 查询
     * @param Request $request
     * @return Response
     * @throws BusinessException
     */
    public function select2(Request $request): Response
    {
        [$where, $format, $limit, $field, $order] = $this->selectInput2($request);
        $where['deleted'] = CommonConstant::DELETE_0;
        $query = $this->doSelect($where, $field, $order);
        $query->select(['id','name','key']);
        return $this->doFormat($query, $format, $limit);
    }

    /**
     * 插入
     * @param Request $request
     * @return Response
     * @throws BusinessException
     */
    public function insert(Request $request): Response
    {
        if ($request->method() === 'POST') {
            return parent::insert($request);
        }
        return view('v1-server/insert');
    }

    /**
     * 更新
     * @param Request $request
     * @return Response
     * @throws BusinessException
     */
    public function update(Request $request): Response
    {
        if ($request->method() === 'POST') {
            return parent::update($request);
        }
        return view('v1-server/update');
    }

    /**
     * 删除
     * @param Request $request
     * @return Response
     * @throws BusinessException
     */
    public function delete(Request $request): Response
    {
        $ids = $this->deleteInput($request);
        $this->doDeleted($ids);
        return $this->json(0);
    }


    /**
     * 批量修改
     * @param Request $request
     * @return Response
     */
    public function batch_update(Request $request): Response
    {
        if ($request->method() === 'POST') {
            $primary_key = $this->model->getKeyName();
            $ids = $request->post($primary_key); // 多个英文逗号隔开
            $multi_port = $request->post('multi_port');
            $domain_port = $request->post('domain_port');
            $link_url_number = $request->post('link_url_number');
            $bandwidth_up = $request->post('bandwidth_up');
            $bandwidth_down = $request->post('bandwidth_down');
            $max_limit = $request->post('max_limit');
            if (!$ids) {
                return $this->json(1, '服务器参数错误！');
            }
            $data = [];
            if ($multi_port) {
                $data['multi_port'] = $multi_port;
            }
            if ($domain_port) {
                $data['domain_port'] = $domain_port;
            }
            if ($link_url_number) {
                $data['link_url_number'] = $link_url_number;
            }
            if ($bandwidth_up) {
                $data['bandwidth_up'] = $bandwidth_up;
            }
            if ($bandwidth_down) {
                $data['bandwidth_down'] = $bandwidth_down;
            }
            if ($max_limit) {
                $data['max_limit'] = $max_limit;
            }
            if (!$data) {
                return $this->json(1, '没有输入数字！');
            }
            $this->model->whereIn($primary_key, array_filter(explode(',', $ids)))->update($data);
            return $this->json(0);
        }
        return view('v1-server/batch_update');
    }

    /**
     * 重启
     * @param Request $request
     * @return Response
     */
    public function reboot_server(Request $request): Response
    {
        if ($request->method() === 'POST') {
            $primary_key = $this->model->getKeyName();
            $ids = $request->post($primary_key);
            if (!$ids) {
                return $this->json(1, '服务器参数错误！');
            }
            ServerService::rebootServer($ids);
            return $this->json(0);
        }
    }

    /**
     * 访问
     * @param Request $request
     * @return Response
     **/
    public function visit_server(Request $request): Response{

    }

}
